<template>
  <el-dialog
    v-model="dialogFormVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    title="合同状态变更"
    width="600"
  >
    <!-- 查询条件 -->
    <avue-form
      :option="formOption"
      v-model="formParams"
      v-if="formParams"
      ref="ruleFormRef"
    >
      <template #menu-form="{}">
        <div class="text-right">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="handleSave">保存</el-button>
        </div>
      </template>
    </avue-form>
  </el-dialog>
</template>
<script setup name="ContractStatuDialog">
import {
  contract_status //合同状态
} from '@/hooks/dict/useProject'
import { reactive, getCurrentInstance, watchEffect, useAttrs } from 'vue'
import { contractInfoUpdate } from '@/api/salesManage/contractManage'
const { proxy } = getCurrentInstance()
const attrs = useAttrs()
const props = defineProps({
  contractParam: {
    required: true
  }
})
const data = reactive({
  dialogFormVisible: true,
  formParams: {},
  formOption: {
    submitBtn: false,
    emptyBtn: false,
    column: [
      {
        label: '合同名称',
        prop: 'contractName',
        placeholder: '请输入项目名称或编号',
        type: 'input',
        disabled: true,
        span: 24,
        row: true
      },
      {
        prop: 'status',
        label: '合同状态',
        placeholder: '请选择合同状态',
        type: 'select',
        row: true,
        span: 24,
        dicData: contract_status
      },
      {
        prop: 'updateRemark',
        label: '变更原由',
        row: true,
        span: 24,
        placeholder: '请输入变更原由',
        type: 'textarea',
        rows: 5
      }
    ]
  }
})
const { dialogFormVisible, formParams, formOption } = toRefs(data)

watch(
  () => props.contractParam,
  val => {
    dialogFormVisible.value = props.contractParam.visible
    formParams.value = { ...props.contractParam }
  },
  {
    deep: true,
    immediate: true
  }
)
// 保存
const handleSave = () => {
  let param = JSON.parse(JSON.stringify(formParams.value))
  delete param.visible
  delete param.$status
  proxy.$refs['ruleFormRef'].validate((valid, done, msg) => {
    if (valid) {
      contractInfoUpdate(param).then(res => {
        proxy.$modal.msgSuccess('操作成功')
        if (param.status != '2') {
          done()
        }
        handleClose()
        //调爷组件的ProjectDetails 的方法
        attrs.onContractOK('contractStatus')
      })
    }
  })
}
const emit = defineEmits(['close'])
// 取消
function handleClose() {
  dialogFormVisible.value = false
  emit('close')
}
</script>
